package cn.shyouchi.wuguanjia.android.ui.event.arrange;

import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;

/**
 * Created by Eenie on 2016/11/26 22:06.
 * Des:
 */

public class SelRankAdapter extends BaseQuickAdapter<PersonRankAdapter,BaseViewHolder> {
    private int rankid;
    private int count;

    public SelRankAdapter(List<PersonRankAdapter> data, int rankid,int count) {
        super(R.layout.item_event_sel_rank_arrange_layout, data);
        this.rankid = rankid;
        this.count = count;

    }

    @Override
    protected void convert(final BaseViewHolder holder, final PersonRankAdapter person) {
        final CheckBox radioButton = holder.getView(R.id.ck_state);
        radioButton.setText(person.getPerson().getUser_name());
        radioButton.setChecked(person.isChecked());
        holder.setText(R.id.tv_count, String.valueOf(person.getRankCount(rankid)));
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshPersonArrange(person, holder);
            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshPersonArrange(person, holder);
            }
        });

    }


    private void refreshPersonArrange(PersonRankAdapter person,BaseViewHolder holder) {
        if (person.getRankCount(rankid) > 0) {

                person.setChecked(!person.isChecked());
                notifyItemChanged(holder.getAdapterPosition());


        } else {
            person.setChecked(false);
            notifyItemChanged(holder.getAdapterPosition());
            ToastUtil.showToast("该员工没有可分配工时");
        }
    }

    public List<PersonRankAdapter.Person>  getSelPersons() {
        List<PersonRankAdapter.Person> persons = new ArrayList<>();
        for (PersonRankAdapter adapter : getData()) {
            if (adapter.isChecked()) {
                persons.add(adapter.getPerson());
            }
        }
        return persons;
    }



}
